#' Distance in miles from lat/long
#'
#' This function takes two locations lat/lon and returns their distance in miles
#' 
#' @param lat1 lattitude of location 1
#' @param lon1 longitude of location 1
#' @param lat2 lattitude of location 2
#' @param lon2 longitude of location 2
#' 



distance <- function(lat1, lon1, lat2, lon2, R = 3961){
  
  # convert degrees to radians
  
  lat1 = lat1 * (pi/180)
  lon1 = lon1 * (pi/180)
  lat2 = lat2 * (pi/180)
  lon2 = lon2 * (pi/180)
  
  a = (sin((lat2 - lat1)/2))^2 + cos(lat1) * cos(lat2) * (sin((lon2 - lon1)/2))^2
  b = 2 * atan2(sqrt(a), sqrt(1-a))
  
  return(R * b)
}